const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_list: [],
        limit: 10,
        page: 1,
        have_more: true,
        request_lock: false,
        need_toast: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 验证登录
        app.verifyLogin();

        // 获取订单列表
        this.data.page = 1;
        this.data.need_toast = false;
        app.execute(function(){
            let page = app.getPage();
            page.get_orderlist();
        });
        
    },

    /**
     * 监听用户下拉动作
    */
    onPullDownRefresh: function () {
        this.data.page = 1;
        this.data.need_toast = true;
        app.execute(function () {
            let page = app.getPage();
            page.get_orderlist();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.need_toast = false;
        this.data.page = this.data.page + 1;
        this.get_orderlist();
    },

    /**
     * 获取列表
    */
    get_orderlist: function () {
        if((1 !== this.data.page) && (false === this.data.have_more)){
            wx.showToast({
                title: '没有更多的数据了',
                icon: 'none'
            });
            return;
        };
        wx.showLoading({
            title: '更新列表...',
        });
        wx.request({
            url: app.data.service_url + 'staff_recycle_order/list_unaccept',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'GET',
            data: {
                page: this.data.page,
                limit: this.data.limit
            },
            success: function (response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else if (0 === response.data.errno) {
                        let page = app.getPage();
                        let change = new Object();
                        if(1 === page.data.page){
                            change.order_list = response.data.data.order_list;
                        }else{
                            change.order_list = page.data.order_list;
                            change.order_list = change.order_list.concat(response.data.data.order_list);
                        };
                        if (page.data.limit !== response.data.data.order_list.length){
                            change.have_more = false;
                        };
                        page.setData(change);
                        if (true === page.data.need_toast) {
                            wx.showToast({
                                title: '页面已刷新',
                                duration: 1000
                            });
                        };
                        wx.stopPullDownRefresh();
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function () {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    }

});